#include "giflogicalscreendescriptor.h"

namespace WingGif {

GifLogicalScreenDescriptor::GifLogicalScreenDescriptor(QIODevice *stream,
                                                       QObject *parent)
    : QObject(parent) {
  auto bytes = stream->read(7);
  auto pbyte = bytes.data();
  _width = *reinterpret_cast<quint16 *>(pbyte);
  _height = *reinterpret_cast<quint16 *>(pbyte + 2);

  auto packedFields = bytes[4];
  _hasGlobalColorTable = (packedFields & 0x80) != 0;
  _colorResolution = ((packedFields & 0x70) >> 4) + 1;
  _isGlobalColorTableSorted = (packedFields & 0x08) != 0;
  _globalColorTableSize = 1 << ((packedFields & 0x07) + 1);
  _backgroundColorIndex = bytes[5];
  _pixelAspectRatio = bytes[5] == char(0) ? 0.0 : (15 + bytes[5]) / 64.0;
}

int GifLogicalScreenDescriptor::width() { return _width; }

int GifLogicalScreenDescriptor::height() { return _height; }

bool GifLogicalScreenDescriptor::hasGlobalColorTable() {
  return _hasGlobalColorTable;
}

int GifLogicalScreenDescriptor::colorResolution() { return _colorResolution; }

bool GifLogicalScreenDescriptor::isGlobalColorTableSorted() {
  return _isGlobalColorTableSorted;
}

int GifLogicalScreenDescriptor::globalColorTableSize() {
  return _globalColorTableSize;
}

int GifLogicalScreenDescriptor::backgroundColorIndex() {
  return _backgroundColorIndex;
}

double GifLogicalScreenDescriptor::pixelAspectRatio() {
  return _pixelAspectRatio;
}

} // namespace WingGif
